package Lambda2;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * 函数式接口：当一个接口中，只有一个抽象方法时
 */

//无返回值无参数
@FunctionalInterface
interface NoParameterNoReturn {
    void test();
}

//无返回值一个参数
@FunctionalInterface
interface OneParameterNoReturn {
    void test(int a);
}

//无返回值多个参数
@FunctionalInterface
interface MoreParameterNoReturn {
    void test(int a,int b);
}

//有返回值无参数
@FunctionalInterface
interface NoParameterReturn {
    int test();
}

//有返回值一个参数
@FunctionalInterface
interface OneParameterReturn {
    int test(int a);
}

//有返回值多参数
@FunctionalInterface
interface MoreParameterReturn {
    int test(int a,int b);
}

public class Test {
    public static void main6(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        map.put("fer",2);
        map.put("yte",5);
        map.put("adf",8);
        map.put("hdg",1);

//        map.forEach(new BiConsumer<String, Integer>() {
//            @Override
//            public void accept(String s, Integer integer) {
//                System.out.println("key" + s + " value" + integer);
//            }
//        });

        map.forEach((k,v)->System.out.println("key"+ k +"value" + v));
    }
    public static void main5(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("hello");
        list.add("one");
        list.add("aoe");
        list.add("LeetCode");
        list.add("coder");

//        list.forEach(new Consumer<String>() {
//            @Override
//            public void accept(String s) {
//                System.out.println(s);
//            }
//        });
        list.forEach(s-> System.out.println(s));
        System.out.println("===================");

//        list.sort(new Comparator<String>() {
//            @Override
//            public int compare(String o1, String o2) {
//                return o1.compareTo(o2);
//            }
//        });

        list.sort((o1,o2)->o1.compareTo(o2));

        list.forEach(s-> System.out.println(s));
    }


    public static void main4(String[] args) {
        //非Lambda表达式
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });

        //Lambda表达式
        PriorityQueue<Integer> maxHeap1 = new PriorityQueue<>((x,y)->{return x.compareTo(y);});
        //简写
        PriorityQueue<Integer> maxHeap2 = new PriorityQueue<>((x,y)->x.compareTo(y));
    }
    public static void main3(String[] args) {
        int a = 10;
        //a = 999;
        //匿名内部内中如果要捕获这个变量，这个变量一定是不能被修改的
        NoParameterNoReturn noParameterNoReturn = new NoParameterNoReturn() {
            @Override
            public void test() {
                //此时捕获的变量一定是没有被修改过的，或者是一个常量
                System.out.println("a = " + a);
            }
        };

        noParameterNoReturn.test();
    }
    public static void main2(String[] args) {
        OneParameterNoReturn oneParameterNoReturn = (x)->{
            System.out.println(x);
        };
        oneParameterNoReturn.test(10);

        System.out.println("===================");

        MoreParameterNoReturn moreParameterNoReturn = (x,y)->{
            System.out.println(x+y);
        };

        moreParameterNoReturn.test(10,20);

        System.out.println("================");

        NoParameterReturn noParameterReturn = ()->{
            return 100;
        };
        System.out.println(noParameterReturn.test());

        System.out.println("==================");

        OneParameterReturn oneParameterReturn = x->x*2;
        System.out.println(oneParameterReturn.test(10));

        System.out.println("==================");

        MoreParameterReturn moreParameterReturn = (x,y)->{
            return x*y;
        };
        System.out.println(moreParameterReturn.test(20, 30));

    }
    public static void main1(String[] args) {
        //非Lambda方式
        NoParameterNoReturn noParameterNoReturn = new NoParameterNoReturn() {
            @Override
            public void test() {
                System.out.println("123");
            }
        };

        noParameterNoReturn.test();

        System.out.println("========================");
        //Lambda方式
        NoParameterNoReturn noParameterNoReturn1 = ()-> System.out.println("123");
        noParameterNoReturn1.test();
    }


}




